package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.result.PageResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    public void calliaction(){
        System.out.println("用户注销");
        System.out.println("注销功能bug修复");
        System.out.println("紧急修复bug");
    }
    public void queryById(){
        System.out.println("主页bug查询修复");
        System.out.println("查询主页");
        System.out.println("优化mysql查询");
        System.out.println("紧急修复bug");
    }
    public void register(){
        System.out.println("用户注册");
        System.out.println("注册功能bug修复");
        System.out.println("紧急修复bug");
    }

    @Override
    public void save(Employee employee, Long[] ids) {
        employeeMapper.insert(employee);
        saveRelation(employee, ids);
    }

    private void saveRelation(Employee employee, Long[] ids) {
        if (ids != null && ids.length > 0) {
            for (Long employeeId : ids) {
                employeeMapper.insertRelation(employee.getId(), employeeId);
            }
        }
    }

    // 删除
    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
        employeeMapper.deleteRelation(id);
    }

    // 修改
    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        employeeMapper.deleteRelation(employee.getId());
        saveRelation(employee, ids);

    }



    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        return new PageInfo<>(employeeMapper.selectForList(qo));
    }

    @Override
    public Employee login(String username, String password) {
        Employee employee = employeeMapper.selectByUsernameAndpassword(username, password);
        if (employee == null){
            throw new RuntimeException("账号或密码不正确");
        }
        return employee;
    }


}
